package zhihui.mobile.ui.shop.address;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.zgm.utils.ui.UiTextHeader;

import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.address.ShoppingAddress;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.presenter.shop.ShopAddressManagerPresenter;
import zhihui.mobile.ui.BaseActivity;

/**
 * Created by 张公铭 on 2017/8/15 0015.
 */
public class ShopAddressManagerActivity extends BaseActivity implements IShopAddressManagerContract.IShopAddressManagerView
{
    private IShopAddressManagerContract.IShopAddressManagerPresenter addressManagerPresenter = new ShopAddressManagerPresenter();
    private UiTextHeader mheader;
    private Button btn_add;
    private ListView lv_content;
    private LinearLayout rl_normal;
    private Button btn_normal_add;
    private ShopAddressManagerItemAdapter adapter;

    @Override
    public void initView()
    {
        mheader = (UiTextHeader) findViewById(R.id.shop_address_manager_header);
        mheader.setRightText("");
        mheader.setTitle("地址管理");
        mheader.setLeftBottonIcon(R.drawable.shop_back);
        mheader.setLeftButtonOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                goBack(ShopAddressManagerActivity.class);
            }
        });

        btn_add = (Button) findViewById(R.id.btn_shop_address_manager_add);
        btn_add.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(ShopAddressManagerActivity.this, ShopAddressAddActivity.class);
                startActivity(intent);
            }
        });

        lv_content = (ListView) findViewById(R.id.lv_shop_address_content);
        lv_content.setVisibility(View.GONE);
        adapter = new ShopAddressManagerItemAdapter(this);
        adapter.setListener(new ShopAddressManagerItemAdapter.IShopAddressItemListener()
        {
            @Override
            public void delete(ShoppingAddress address)
            {
                addressManagerPresenter.delete(address);
            }

            @Override
            public void update(ShoppingAddress address)
            {
                Intent intent = new Intent(ShopAddressManagerActivity.this, ShopAddressAddActivity.class);
                intent.putExtra("update_address_id", address.getId());
                startActivity(intent);
            }

            @Override
            public void setDefault(ShoppingAddress address, boolean defaultValue)
            {
                if (defaultValue)
                {
                    addressManagerPresenter.setDefault(address, defaultValue);
                }

            }
        });
        lv_content.setAdapter(adapter);

        rl_normal = (LinearLayout) findViewById(R.id.ll_shop_address_manager_normal);
        rl_normal.setVisibility(View.VISIBLE);

        btn_normal_add = (Button) findViewById(R.id.btn_shop_address_manager_normal_add);
        btn_normal_add.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(ShopAddressManagerActivity.this, ShopAddressAddActivity.class);
                startActivity(intent);
            }
        });

        addressManagerPresenter.initData();
    }

    @Override
    protected int getLayoutResId()
    {
        return R.layout.shop_address_manager_activity;
    }

    @Override
    protected IPresenter[] getPresenters()
    {
        return new IPresenter[]{addressManagerPresenter};
    }

    @Override
    protected void onInitPresenter()
    {
        addressManagerPresenter.init(this, this);
    }

    @Override
    public void refreshList(List<ShoppingAddress> list)
    {
        if (list.size() > 0)
        {
            rl_normal.setVisibility(View.GONE);
            lv_content.setVisibility(View.VISIBLE);
            adapter.setList(list);
            adapter.notifyDataSetChanged();
        }
    }
}
